<%@ page language="java" %>

<%@ page import="com.scalar.chiptrack.commons.SessionKeys,
                 com.scalar.chiptrack.utils.ConfigManager,
				 com.scalar.chiptrack.commons.Constants,
				 com.scalar.chiptrack.users.Functionality,
				 com.scalar.chiptrack.users.User,
                 java.util.StringTokenizer,
                 java.util.ArrayList,
                 java.util.Iterator"%>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>
<chiptrack:checkLogon sourcePage="/assmRelease.do" />
<%String chipTracContext = "/" + ConfigManager.getChipTracContext(); %>

<HTML>
<HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet"></HEAD>
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/assembly_release.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/quickSearch.js"></script>

<SCRIPT language="JavaScript">
<%
    String maxFabLotsForAssemblyRelease = ( String ) request.getAttribute("maxFabLotsForAssemblyRelease");
    maxFabLotsForAssemblyRelease = ( maxFabLotsForAssemblyRelease == null ) ? "0" : maxFabLotsForAssemblyRelease.trim();
%>

var maxFabLotsForAssemblyRelease = <%=maxFabLotsForAssemblyRelease%>;
var lotsCount = -1;
window.onload = init;
var form;

function init()
{
    qs_1 = new QuickSearch(document.assmReleaseForm.searchFGProcessstepId, document.assmReleaseForm.qs1, false);
    form = document.assmReleaseForm;
    disableBinsForLots();
}
function loadComments( partNumber )
{
    var selectedIndex = partNumber.selectedIndex-1;
    document.assmReleaseForm.comments.value = '';
    if( comments != null && selectedIndex >= 0)
    {
        document.assmReleaseForm.comments.value = comments[selectedIndex];
    }

}

function searchRelease( isTurnKeySelected )
{
    var errors = '';

    if( document.assmReleaseForm.searchFGProcessstepId.value == -1)
    {
		errors = errors + "<li> Please select a Part Number";
    }
    if( document.assmReleaseForm.searchFromVendorId.value == -1)
    {
		errors = errors + "<li> Please select a Die Bank Vendor";
    }
    if( document.assmReleaseForm.searchToVendorId.value == -1)
    {
		errors = errors + "<li> Please select a Assembly Release to Vendor";
    }
    if( document.assmReleaseForm.searchShipToVendorId.value == -1)
    {
		errors = errors + "<li> Please select a Ship To Vendor";
    }
    if( errors != '' )
    {
        var pa = document.getElementById("errors");
        pa.innerHTML = errors;
        document.assmReleaseForm.searchTurnKey.checked = false;
        return;
    }
    else
    {
        if( isTurnKeySelected )
        {
            document.assmReleaseForm.action = '<%=chipTracContext%>/newAssmRelease.do?action=loadLots';
        }
        else
        {
            document.assmReleaseForm.action = '<%=chipTracContext%>/newAssmRelease.do?action=turnKeySelected';    
        }
        document.assmReleaseForm.submit();
    }
}

function partNumberChanged()
{
    var turnKeyLabel = document.getElementById("turnKeyLabel");
    turnKeyLabel.innerHTML = "Assembly";

    document.assmReleaseForm.searchTurnKey.checked = false;
}


function createNewAssemblyRelease()
{
    if( lotsCount != -1 )
    {
        var lotIdsSelected = form['lotId'];
        var fabLotsSelectedCount = 0;
        var errMsg_PleaseSelectALot="Please select a Lot "
        var errMsg_MoreFabLotsSelected="Number of lots selected must not be more than "
        var errMsg_selectAtleastOneBinForLot="Please select atleast one bin for Lot Number "
        if( !lotIdsSelected.length )
        {
            var isLotIdSelected = lotIdsSelected.checked;
            if( !isLotIdSelected )
            {
                var pa=document.getElementById("errors");
                pa.innerHTML = "<li> "+errMsg_PleaseSelectALot;
                return;
            }
            fabLotsSelectedCount++;
            if( fabLotsSelectedCount >  maxFabLotsForAssemblyRelease)
            {
                var pa=document.getElementById("errors");
                pa.innerHTML = "<li> "+errMsg_MoreFabLotsSelected+maxFabLotsForAssemblyRelease ;
                return;
            }
            var lotId = lotIdsSelected.value;
            /*if( !checkIfAtleastOneBinIsSelectedForThisLot( lotId ) )
            {
                var pa=document.getElementById("errors");
                var lotNumber = form['lotNumberForLotId'+lotId].value
                pa.innerHTML = "<li>"+errMsg_selectAtleastOneBinForLot+"'"+lotNumber+"'";
                return;
            }*/
        }
        else
        {
            var atleastOneLotSelected = false;
            for( var i=0; i<lotIdsSelected.length; i++ )
            {
                if( lotIdsSelected[i].checked )
                {
                    atleastOneLotSelected = true;
                    fabLotsSelectedCount++;
                }
            }
            if( !atleastOneLotSelected )
            {
                var pa=document.getElementById("errors");
                pa.innerHTML = "<li> "+errMsg_PleaseSelectALot;
                return;
            }

            if( fabLotsSelectedCount >  maxFabLotsForAssemblyRelease)
            {
                var pa=document.getElementById("errors");
                pa.innerHTML = "<li> "+errMsg_MoreFabLotsSelected+maxFabLotsForAssemblyRelease ;
                return;
            }
            /*for( var i=0; i<lotIdsSelected.length; i++ )
            {
                if( lotIdsSelected[i].checked )
                {
                    atleastOneLotSelected = true;
                    var lotId = lotIdsSelected[i].value;
                    if( !checkIfAtleastOneBinIsSelectedForThisLot( lotId ) )
                    {
                        var pa=document.getElementById("errors");
                        var lotNumber = form['lotNumberForLotId'+lotId].value
                        pa.innerHTML = "<li> "+errMsg_selectAtleastOneBinForLot+"'"+lotNumber+"'";
                        return;
                    }
                }
            }*/
        }
        document.assmReleaseForm.action = '<%=chipTracContext%>/assmRelease.do?action=new'
        document.assmReleaseForm.submit();
    }
}

function checkIfAtleastOneBinIsSelectedForThisLot( lotId )
{
    var selectedBins = form['BinsForLotId'+lotId]
    if( !selectedBins.length )
    {
        if( selectedBins.checked )
        {
            return true;
        }
        return false;
    }
    else
    {
        for( var i=0; i<selectedBins.length; i++)
        {
            var selectedBinCtrl = selectedBins[i]
            if( selectedBinCtrl.checked )
            {
                return true;
            }
        }
        return false;
    }
}        

function disableBinsForLots()
{
    if( lotsCount != -1 )
    {
        var lotIds = form['lotId'];
        if( !lotIds.length )
        {
            enableOrDisableBinsForLot( lotIds.value, !lotIds.checked )
        }
        else
        {
            for( var i=0; i<lotIds.length; i++ )
            {
                enableOrDisableBinsForLot( lotIds[i].value, !lotIds[i].checked )
            }
        }
    }
}

function enableOrDisableBinsForLot( lotId, isDisable)
{
    var bins = form['BinsForLotId'+lotId]
    if( !bins.length )
    {
        bins.disabled = isDisable;
        if( isDisable )
        {
            bins.checked = false;
        }
    }
    else
    {
        for( var i=0; i<bins.length; i++)
        {
            bins[i].disabled = isDisable;
            if( isDisable )
            {
                bins[i].checked = false;
            }
        }
    }
}

function resetForm()
{
    document.assmReleaseForm.reset();
    //loadComments(document.assmReleaseForm.fgProcessstepid);
    qs_1.resetQuickSearchCntrl();
}
function quickSearchLink()
{
 quickSearch(qs_1);
 //loadComments(document.assmReleaseForm.fgProcessstepid);
}
function getTransitLots()
{
    newWindow = window.open ('<%=chipTracContext%>/newAssmRelease.do?action=showTransitLots','transitLotsPopup','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=yes,width=650,height=500,resizable=yes');
    newWindow.focus();
}
</SCRIPT>

<BODY bgColor="#ffffff" leftMargin="0" topMargin="0" marginheight="0" marginwidth="0">

<!-- Top Header Start-->
<%@ include file="/jsp/includes/header.jsp" %>
<!-- Top Header End-->

<table cellSpacing=0 cellPadding=0 width="100%" border=0>
    <tr>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
            <!-- Side Navigation start -->
	        <%@ include file="/jsp/operations/releaseMenu.jsp" %>
	        <!-- end of the side navigations -->
        </td>
	    <td bgcolor="#ffffff">
	        <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></td>
	        <td bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></td>
	        <td valign="top" width="100%">
            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
	        <BR>
	        <html:form action="/newAssmRelease.do" method="POST" type="com.scalar.chiptrack.operations.release.forms.AssmReleaseForm">
            <table width="90%" align="center" cellpadding="2" cellspacing="2" border="0">
			    <tr><td colspan="4" class="NArialW" align="center">Assembly Release</td></TR>
                <tr><td colspan='4' align="left" class='NArialL'>&nbsp;<FONT id="errors" color='FF0000'></font></td></tr>
                <tr><td colspan='4' class="NArialL"><chiptrack:errors styleClass="NArialL"/></td></tr>
                <tr><td colspan='4' class="NArialL"><chiptrack:info styleClass="NArialL"/></td></tr>
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='5'></td></tr>
                <tr>
                    <td class="NArialL" width='20%'>
                    Quick Search Part Number:
                    </td>
                    <td class="NArialL" width='30%'>
                    <input type="text" name="qs1" class="txfield" size='30' onkeyup='quickSearchLink()'/>
                    </td>
                    <td colspan='2' width='50%'>&nbsp;</td>
                </tr>
                <tr>
                    <td class="NArialL">
                        FG Part Number:
                    </td>
                    <td class="NArialL">
                        <html:select name="assmReleaseForm" property="searchFGProcessstepId"  styleClass="txfield" onchange="javascript:partNumberChanged();">
                            <html:option value="-1">--Select Part Number--</html:option>
                            <html:options collection="partNumberList" property="value" labelProperty="label"/>
                        </html:select>
                        <html:hidden property="searchProcessstepId" />
                        <html:hidden property="searchTurnKeyLabel" />
                    </td>
                    <td class="NArialL">
                        Assembly Release to Vendor:
                    </td>
                    <td class="NArialL">
                        <html:select name="assmReleaseForm" property="searchToVendorId"  styleClass="txfield" >
                            <html:option value="-1">--Select Vendor--</html:option>
                            <html:options collection="toLocationList" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                </tr>
                <%--<tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='5'></td></tr>--%>
                <tr>
                    <td class="NArialL">
                        Die Bank Vendor:
                    </td>
                    <td class="NArialL">
                        <html:select name="assmReleaseForm" property="searchFromVendorId"  styleClass="txfield" >
                            <html:option value="-1">--Select Vendor--</html:option>
                            <html:options collection="fromLocationList" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                    <td class="NArialL">
                        <%--Ship To Vendor:--%>
                        After Completion Vendor:
                    </td>
                    <td class="NArialL">
                        <html:select name="assmReleaseForm" property="searchShipToVendorId"  styleClass="txfield" >
                            <html:option value="-1">--Select Vendor--</html:option>
                            <html:options collection="shipToLocationList" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                </tr>
                <%--<tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='5'></td></tr>--%>
                <tr>
                    <td class="NArialL">
                        <%--Base Product Name/Package Type:--%>
                         TurnKey:
                    </td>
                    <td class="NArialL">
                        <%--<input type="text" name="comments" class='txfield' disabled size="25">--%>
                        <html:checkbox property="searchTurnKey" styleClass="txfield" onclick="javascript:searchRelease(0);"/>
                        <font id="turnKeyLabel">
                            <bean:write name="assmReleaseForm" property="searchTurnKeyLabel" />
                        </font>
                    </td>
                    <td class="NArialL">
                         Release Type:
                    </td>
                    <td class="NArialL">
                        <html:select name="assmReleaseForm" property="searchRequestType" styleClass="txfield" >
                            <%--<html:option value="-1">--Select Req Type--</html:option>--%>
                            <html:options collection="requestTypes" property="value" labelProperty="label"/>
                        </html:select>
                    </td>
                </tr>
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='15'></td></tr>
                <tr>
                    <td colspan="4" align="center">
                    <a href="javascript:searchRelease(1);" onmouseover="window.status='Click to Create new Assembly Release';return true" onmouseout="window.status='';return true">
                        <img border="0" src="<%=chipTracContext%>/jsp/includes/images/submit.gif" title="Click here to Create New Assembly Release"/></a>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <a href="javascript:resetForm();"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/reset.gif" width="53" height="17" onmouseover="window.status='Click to Reset';return true" onmouseout="window.status='';return true" title="Click here to Reset"></a>
                    </td>
                </tr>
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>

                <TR><td colspan="4" class="NArialL">Please <A href="javascript:getTransitLots()" class="leftsection">Click here</A> to receive the Tranist Lots</td></TR>
                <logic:present name="lotsList">
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>
                <tr>
                    <td colspan='4' class="NArialL">
                        Please <a href="javascript:createNewAssemblyRelease()" class="leftsection" onMouseOver="window.status='Click to generate Assembly Release';return true" onMouseout="window.status='';return ">Click here</a> to generate Assembly Release for the selected lots
                    </td>

                </tr>
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>
                <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='10'></td></tr>    
                <TR>
                    <td colspan='4'>
                    <P class=NArialS><img border="0" src="<%=chipTracContext%>/jsp/includes/images/exclamation-46x47.gif" alt="" width="24" height="24" align="middle"/>
                        Lots marked in
                        <font color='red' size="3"><b>&nbsp;* &nbsp;</b></font> (Available Binning Qty does not match Vendor Qty)
                        <font color='blue' size="2"><b>&nbsp;&nbsp;# &nbsp;&nbsp;</b></font> (Binning not received from Vendor)
                    </P>
                    </td>
                </TR>
                <tr>
                    <td colspan='4'>
                        <table width="100%" align="center" cellpadding="2" cellspacing="2" border="0">
                            <tr>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                    <font color="#FFFFFF">Select Lot</font>
                                </td>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                   <font color="#FFFFFF">Fab Lot Number</font>
                                </td>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                   <font color="#FFFFFF">Lot Number</font>
                                </td>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                   <font color="#FFFFFF">Wafers Count</font>
                                </td>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                   <font color="#FFFFFF">Total Die Available</font>
                                </td>
                                <td class="NArialL" bgcolor="#993333" align="center">
                                   <font color="#FFFFFF">Die Qty(Vendor)</font>
                                </td>
                            </tr>
                            <logic:iterate name="lotsList" id="assemblyReleaseLot" type="com.scalar.chiptrack.operations.release.ReleaseLot" indexId="iteratorIdx" scope='request'>
                                <tr <%if(iteratorIdx.intValue()%2==0){%> bgcolor='#DEE1E2' <% } else {%> bgcolor='#FFFFEE' <%}%>>
                                    <td class="NArialL" align="center">
                                        <%--<bean:write name="assemblyReleaseLot" property="lotNumber" />--%>
                                        <input type='checkbox' name="lotId" class="txfield" value="<bean:write name='assemblyReleaseLot' property='lotId'/>" onclick='enableOrDisableBinsForLot(this.value, !this.checked)'/>
                                    </td>
                                    <%
                                        StringTokenizer qt = new StringTokenizer(assemblyReleaseLot.getAvailableQtyStr(), "&" );
                                    %>
                                    <td class="NArialL" align="center">
                                        <logic:equal value="true" name="assemblyReleaseLot" property="discrepanyBinning"><font color="red" size="2"><b>*</b></font></logic:equal>
                                        <logic:equal value="0" name="assemblyReleaseLot" property="binInfoUpdate"><font color="blue" size="2"><b>#</b></font></logic:equal>
                                        <bean:write name="assemblyReleaseLot" property="fabLotNumber"/>
                                    </td>

                                    <td class="NArialL" align="center">
                                        <%--<a href="javascript:createNewAssemblyRelease(<bean:write name="assemblyReleaseLot" property="lotId" />, '<bean:write name="assemblyReleaseLot" property="lotNumber" />')" onMouseOver="window.status='Click to Generate Assembly Release';return true" onMouseout="window.status='';return ">Generate</a>--%>
                                        <bean:write name="assemblyReleaseLot" property="lotNumber" />
                                        <input type='hidden' name="lotNumberForLotId<bean:write name='assemblyReleaseLot' property='lotId'/>" value='<bean:write name='assemblyReleaseLot' property='lotNumber'/>'/>

                                        <logic:iterate name="assemblyReleaseLot" property="goodBinsList" id="bin" type="java.lang.String">
                                            <input type="hidden" class="txfield" name="BinsForLotId<bean:write name='assemblyReleaseLot' property='lotId'/>" value="<bean:write name="bin"/>"/
                                        </logic:iterate>
                                    </td>
                                    <%--<td  class="NArialL" align="left">
                                        <logic:iterate name="assemblyReleaseLot" property="goodBinsList" id="bin" type="java.lang.String">
                                            <input type="checkbox" class="txfield" name="BinsForLotId<bean:write name='assemblyReleaseLot' property='lotId'/>" value="<bean:write name="bin"/>"/>&nbsp;(<%=qt.nextToken()%>)&nbsp;Sort&nbsp;<bean:write name="bin"/>&nbsp;
                                        </logic:iterate>
                                    </td>--%>
                                    <td class="NArialL" align="center">
                                        <bean:write name="assemblyReleaseLot" property="waferIdsCount" />
                                    </td>
                                    <td class="NArialL" align="center">
                                        <bean:write name="assemblyReleaseLot" property="qtyAvailable" />
                                    </td>
                                     <td class="NArialL" align="center">
                                        <bean:write name="assemblyReleaseLot" property="dieQty"/>
                                    </td>
                                </tr>
                                <script>
                                    lotsCount = <%=iteratorIdx%>;
                                </script>
                            </logic:iterate>
                            <tr><td colspan='4'><IMG src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='5'></td></tr>

                        </table>
                    </td>
                </tr>
                </logic:present>
            </TABLE>
	        </html:form>
            <!-- till here -->
            <BR><BR><BR>
        </td>
        <td bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></td>
    </tr>
</TABLE>

<!-- Footer starts here -->
<%@ include file="/jsp/includes/footer.jsp" %>
<!-- footer end here -->

</BODY>
</HTML>